<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="Generator" content="ALORK 20150428.01" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="Keywords" content="<?php echo $this->_var['keywords']; ?>" />
<meta name="Description" content="<?php echo $this->_var['description']; ?>" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<meta name="viewport" content="user-scalable=no, width=device-width, initial-scale=1.0, maximum-scale=1.0"/>

<title><?php echo $this->_var['page_title']; ?></title>

<link rel="shortcut icon" href="favicon.ico" />
<link rel="icon" href="animated_favicon.gif" type="image/gif" />
<link rel="stylesheet" href="./public/my_style.css" />
<script type="text/javascript" src="./../js/mobile-common.js"></script>
<?php echo $this->smarty_insert_scripts(array('files'=>'./../js/utils.js,./../js/user.js,./../js/jquery.js,./../js/jquery.json-1.3.js,./../js/region.js,./../js/shopping_flow.js,./../js/transport.js,./../js/common.js')); ?>
</head>
<body class="adapt">
<?php echo $this->fetch('library/user_head.lbi'); ?>
<?php if ($this->_var['step'] == "cart"): ?>



<script type="text/javascript">
    <?php $_from = $this->_var['lang']['password_js']; if (!is_array($_from) && !is_object($_from)) { settype($_from, 'array'); }; $this->push_vars('key', 'item');if (count($_from)):
    foreach ($_from AS $this->_var['key'] => $this->_var['item']):
?>
    var <?php echo $this->_var['key']; ?> = "<?php echo $this->_var['item']; ?>";
    <?php endforeach; endif; unset($_from); ?><?php $this->pop_vars();; ?>
</script>
<div  id="cart" >
<form id="formCart" name="formCart" method="post" action="flow.php">
<?php if ($this->_var['goods_list']): ?>
    <div class="cart_box">
        <?php $_from = $this->_var['goods_list']; if (!is_array($_from) && !is_object($_from)) { settype($_from, 'array'); }; $this->push_vars('', 'goods');if (count($_from)):
    foreach ($_from AS $this->_var['goods']):
?>
        <div class="cart_list <?php if (! $this->_var['goods']['is_buy'] || $this->_var['goods']['goods_number'] < 0): ?>no_buy<?php endif; ?>">
        <div class="<?php if (! $this->_var['goods']['is_buy'] || $this->_var['goods']['is_stockout']): ?>unbuy<?php else: ?>goods_id<?php endif; ?>" onclick="<?php if (! $this->_var['goods']['is_buy'] || $this->_var['goods']['is_stockout']): ?>point('此商品不可结算！')<?php endif; ?>">
            <?php if ($this->_var['goods']['is_buy']): ?>
            <input type="checkbox" class="none" id="rec_ids" name="rec_id[]" value="<?php echo $this->_var['goods']['rec_id']; ?>" />
            <?php endif; ?>
        </div>
        <div class="left"><a href="goods.php?id=<?php echo $this->_var['goods']['goods_id']; ?>"><img src="../<?php echo $this->_var['goods']['goods_thumb']; ?>" alt="<?php echo $this->_var['goods']['goods_name']; ?>"  /></a></div>
        <table class="goods_tab" style="width: 180px; height: auto; position: relative;">
            <tr class="goods_name">
                <td><a href="goods.php?id=<?php echo $this->_var['goods']['goods_id']; ?>"><?php echo $this->_var['goods']['goods_name']; ?><?php if ($this->_var['goods']['goods_attr']): ?> (<?php echo $this->_var['goods']['goods_attr']; ?>) <?php endif; ?> </a></td>
                <td>
                    <div class="delete" goods_id="<?php echo $this->_var['goods']['rec_id']; ?>"></div>
                </td>
            </tr>
            <tr class="have_buy">
                <?php if ($this->_var['goods']['is_buy'] && $this->_var['goods']['goods_number'] > 0): ?>
                <td>有货 <?php if ($this->_var['goods']['extension_code'] == 'yugou'): ?><font style="color: red;">【618专场】</font><?php endif; ?></td>
                <?php else: ?>
                <td><?php if ($this->_var['goods']['extension_code'] == 'yugou'): ?><font style="color: red;">【618专场】</font> <?php else: ?>无货<?php endif; ?></td>
                <?php endif; ?>
            </tr>
            <tr class="goods_some">
                <td class="goods_price"><e style="font-size: 12px;">￥</e><span class="price"><?php echo $this->_var['goods']['goods_price']; ?></span>
                    <input type="hidden" class="gp" name="goods_price[<?php echo $this->_var['goods']['rec_id']; ?>]" value= "<?php echo $this->_var['goods']['goods_price']; ?>"/>
                </td>
                <td style="display: none;" align="center" bgcolor="#ffffff" class='subtotals'><span preferential="<?php echo $this->_var['goods']['preferential']; ?>"><?php echo $this->_var['goods']['subtotal']; ?></span></td>
                <td class="goods_num">
                    <div class="cart_num">
                    <?php if ($this->_var['goods']['goods_id'] != 0 && $this->_var['goods']['extension_code'] != 'yugou' && $this->_var['goods']['extension_code'] != 'libao'): ?>
                    <div class="cut" onclick="cart_button($(this),1)">-</div>
                    <input type="text" id="<?php echo $this->_var['goods']['rec_id']; ?>" name="goods_number[<?php echo $this->_var['goods']['rec_id']; ?>]"  class="num_input"  value="<?php echo $this->_var['goods']['goods_number']; ?>" onblur="cart_button($(this),2,'<?php echo $this->_var['user_id']; ?>')"/>
                    <input type="hidden" class="goods_stock" name="stock" class="stock" value="<?php echo $this->_var['goods']['goods_stock']; ?>" />
                    <div class="add" onclick="cart_button($(this),3)">+</div>
                    <?php else: ?>
                    <input type="text" id="<?php echo $this->_var['goods']['rec_id']; ?>" name="goods_number[<?php echo $this->_var['goods']['rec_id']; ?>]"  class="num_input"  value="<?php echo $this->_var['goods']['goods_number']; ?>" readonly="readonly" />
                    <?php endif; ?>
                    </div>
                </td>
            </tr>
        </table>
        </div>
        <?php endforeach; endif; unset($_from); ?><?php $this->pop_vars();; ?>


    </div>




    <div class="flow_button">
        <div class="goods_id_all selecteds" onclick="all_cart(this)">
            <span class="none_all"></span>
        </div>
        <span class="select_all">全选</span>
        <div class="sum_money">
        商品总价：<e style="font-size: 12px;color:#8E1519;">￥</e>
                  <span class="total_price" style="color: #8a0105;">0.00</span>
        </div>
        <input type="hidden" name="step" value="checkout" />
        <div class="right" <?php if ($this->_var['state'] > 0): ?>onclick="$('#mask,#cart_popup').show();"<?php else: ?> onclick="sale_submit();"<?php endif; ?>>去结算</div>
    </div>

    
<?php if ($this->_var['state'] > 0): ?>
    <div id="mask" class="none" onclick="$('#mask,#cart_popup').hide();"></div>
    <div id="cart_popup" class="none">
        <p>
            亲，有挑选到心动的宝贝么？<br />
            <?php if ($this->_var['state'] == 2): ?>
            此次购物您可以享受<span>全场包邮</span>，<br />
            <?php endif; ?>
            <?php if ($this->_var['state'] == 3): ?>
            此次购物您可以享受<span>8折优惠</span>和<span>全场包邮</span>，<br />
            <?php endif; ?>
            快返回商城把更多的商品一起带回家吧！
        </p>
        <div class="button">
            <a class="btn left" href="index.php" title="首页">选购更多</a>
            <input class="btn right" type="button" onclick="sale_submit();" value="继续结算" />
        </div>
    </div>
<?php endif; ?>



</form>

<?php else: ?>
<div class="cart_blank">
    <img src="./images/mobile_cart_blank.png" alt="" />
    <span>购物车还是空的哦！</span>
    <a href="index.php" class="continue">去逛逛...</a>
</div>
<?php endif; ?>
<script type="text/javascript" charset="utf-8">


/* 勾选商品改变显示商品总价 */
$(".goods_id").click(function(){
    var price = parseFloat($(this).parent().find(".price").text());                       //获取勾选商品的价格
    var num   = parseFloat($(this).parent().find(".num_input").val());                    //获取勾选商品的数量
    var total_price = parseFloat($(".total_price").text());  //获取商品总价
    /* 判断勾选状态 */
    //if( $(this).children().attr("checked") == false ){
    if( !$(this).hasClass("selected") ){
        //计算商品总价：加上
        total_price += price * num;
        total_price = toDecimal2(total_price);
        $(".total_price").text(total_price);
        $(this).addClass("selected");
        $(this).children().attr("checked",true);
        if( $(".selected").length == $(".goods_id").length ){
            $(".goods_id_all").addClass("selecteds");
        }
    }else{
        //计算商品总价：减去
        total_price -= price * num;
        total_price = toDecimal2(total_price);
        $(".total_price").text(total_price);
        $(this).removeClass("selected");
        $(this).children().attr("checked",false);
        $(".goods_id_all").removeClass("selecteds");
    }
});

/* 进入购物车就全选商品 */
$(function(){
    all_cart();
});

/* 全选 */
function all_cart(obj){
    if (!$(obj).hasClass("selecteds")) {
        $(obj).addClass("selecteds");
        $(".goods_id").addClass("selected");
        $(".goods_id .none").attr("checked", true);
        //计算并返回总价
        price_count();
    } else {
        $(obj).removeClass("selecteds");
        $('.goods_id').removeClass("selected");
        $(".goods_id .none").attr("checked", false);
        $(".total_price").text("0.00");
    }
}


function price_count(){
    var total_price   = 0;              //总价
    var goods_price   = 0.00;           //商品单格
    var goods_number  = 0;              //商品数量
    $('.selected').each(function(){
        var price = parseFloat($(this).parent(".cart_list").find(".price").text());           // 商品价格
        var num   = parseInt($(this).parent(".cart_list").find(".num_input").val());   // 商品数量
        total_price += price * num;
    });
    $(".total_price").text(toDecimal2(total_price));
}

/* 调整商品价格的显示格式 */
function toDecimal2(x) {
    var f = Math.round(x*100)/100;
    var s = f.toString();
    var rs = s.indexOf('.');
    if (rs < 0) {
        rs = s.length;
        s += '.';
    }
    while (s.length <= rs + 2) {
        s += '0';
    }
    return s;
}

/* 点击商品添加、减少和输入方法 */
function cart_button(obj, num){
    if(num == '1'){

        var val= parseInt($(obj).siblings('.num_input').val());
        var id = parseInt($(obj).siblings('.num_input').attr('id'));
        if(val>1){
            val -= 1;
        }else{
            val = 1;
            return false;
        }
        ajax_update_cart(id, val, $(obj).parents('.cart_list'));
    }
    else if(num == '2'){
        var val=parseInt($(obj).val());
        var id = parseInt($(obj).attr('id'));
        var goods_stock = $(obj).siblings('.goods_stock').val();
        if(val>0 && goods_stock < val){
            point('商品只剩'+goods_stock+'件',1);
            val = goods_stock;
        }
        ajax_update_cart(id, val, $(obj).parents('.cart_list'));
    }else if(num == '3'){
        var val=parseInt($(obj).siblings('.num_input').val());
        var id = parseInt($(obj).siblings('.num_input').attr('id'));
        var goods_stock = $(obj).siblings('.goods_stock').val();
        if(val>0){
            val += 1;
            if(goods_stock < val){
                point('商品只剩'+goods_stock+'件',1);
                val = goods_stock;
                return false;
            }
        }
        ajax_update_cart(id, val, $(obj).parents('.cart_list'));
    }
}

/*
 * 更新购物车
 * 要考虑购买数量达到3的时候单价会改变的情况
 */
function ajax_update_cart(id, number, obj){
    if(id>0 && number>0){
        $(obj).find('.num_input').val(number);
        // 更新购物车
        $.ajax({url:"flow.php?step=ajax_update_cart", type: "POST", data: {id:id, goods_number:number}, async:false,dataType:'json',success:function(data){
            if (data.error == 0) {
                $(obj).find('.price').text(data.content);
                count_price();
            };
        }});
    }else{
        return false;
    }
}

/*计算商品总价*/

function count_price(){
    var total_price = 0;//商品总价
    $('.selected').each(function(){
            var one_price   = $(this).parents('.cart_list').find('.price').text();
            var num = $(this).parents('.cart_list').find('.num_input').val();
            total_price +=one_price*num;
    });
    total_price = toDecimal2(total_price);
    $(".total_price").text(total_price);
}

$("#test").click(function(){
    count_price();




});










/* 删除商品 */
$('.delete').click(function(){
    var a = $(this).attr('goods_id');
    var test="location.href='flow.php?step=drop_goods&amp;id="+a+"'";
    prompt('你确认删除该商品',test);
});

/* 删除礼包 */
$('.activityDeleteCart').click(function(){
    var a=$(this).attr('goods_id');
    var test="location.href='activity.php?act=activityDeleteCart&amp;id="+a+"'";
    prompt('你确认删除该商品',test);
});

/* 结算 */
function sale_submit(){
    if($(".selected").length<=0){
        point("请选择结算商品",1);
        return false
    }
    $("#formCart").submit();
}
</script>
</div>
<?php echo $this->fetch('library/footer.lbi'); ?>
<?php endif; ?>


<?php if ($this->_var['step'] == "complete"): ?>

<div id="done" >
    <div class="complete">
        您已提交成功
    </div>
    <div class="order_number">
        订单号<br />
        <?php echo $this->_var['order']['order_sn']; ?>
        <br />
        <?php if ($this->_var['order']['pay_online']): ?>
        你选择的支付方式为在线支付请在1小时内支付逾期订单无效
        <br />
        <?php else: ?>
        你选择的支付方式为货到付款请保持你的电话畅通
        <br />
        <?php endif; ?>
    </div>
    <div class="complete_button">
        <?php if ($this->_var['order']['pay_online']): ?>
        <?php echo $this->_var['order']['pay_online']; ?>
        <?php else: ?>
        <a href="index.php" class="left">继续购物</a>
        <a href="user.php?act=order_detail&order_id=<?php echo $this->_var['order']['order_id']; ?>" class="right">查看订单</a>
        <?php endif; ?>
    </div>
</div>
<?php endif; ?>

<script type="text/javascript">

    /*字体大小 自适应 rem单位*/
    function auto(){
        var width = $('body').width();
        var font_size = 20*(width/320);
        var font_size2 = (width/320)*625+'%';
        $('body').css('font-size',font_size);
        $('html').css('font-size',font_size2);
    }
        auto();
    /*浏览器横屏坚屏改变时也运行函数*/
    window.onorientationchange = function() {
        auto();
    };
    /* 浏览器窗口发生改变也运行函数*/
    window.resize=function(){
        auto();
    };
</script>
</body>
</html>
